﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI.WebControls;

namespace CMS.Extends
{
    public class Library
    {
        /// <summary>
        /// Phân trang
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="repeaterData"></param>
        /// <param name="repeaterpage"></param>
        /// <param name="pageNumber">số lượng trang</param>
        /// <param name="pageSize">số record trên 1 trang</param>
        public static void PaginateRepeater(DataTable dt, Repeater repeaterData, Repeater repeaterpage, int pageNumber, int pageSize)
        {
            PagedDataSource pdsItem = new PagedDataSource();
            DataView dv = new DataView(dt);
            pdsItem.DataSource = dv;
            pdsItem.AllowPaging = true;
            pdsItem.PageSize = pageSize;
            pdsItem.CurrentPageIndex = pageNumber;
            if (pdsItem.PageCount > 1)
            {
                repeaterpage.Visible = true;
                ArrayList arrPage = new ArrayList();
                for (int i = 0; i < pdsItem.PageCount; i++)
                    arrPage.Add((i + 1).ToString());
                repeaterpage.DataSource = arrPage;
                repeaterpage.DataBind();
            }
            else
            {
                repeaterpage.Visible = false;
                repeaterData.DataSource = pdsItem;
                repeaterData.DataBind();
            }

            repeaterData.DataSource = pdsItem;
            repeaterData.DataBind();
        }

        /// <summary>
        /// MD5 generate
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GennerateToMD5(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bHash = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            StringBuilder sbHash = new StringBuilder();
            foreach (byte b in bHash)
            {
                sbHash.Append(String.Format("{0:x2}", b));
            }
            return sbHash.ToString();
        }
    }
}